<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{&quot;hostname&quot;:&quot;www.nilstorm.com&quot;,&quot;root&quot;:&quot;&#x2F;&quot;,&quot;images&quot;:&quot;&#x2F;images&quot;,&quot;scheme&quot;:&quot;Gemini&quot;,&quot;version&quot;:&quot;8.5.0&quot;,&quot;exturl&quot;:false,&quot;sidebar&quot;:{&quot;position&quot;:&quot;left&quot;,&quot;display&quot;:&quot;post&quot;,&quot;padding&quot;:18,&quot;offset&quot;:12},&quot;copycode&quot;:false,&quot;bookmark&quot;:{&quot;enable&quot;:false,&quot;color&quot;:&quot;#222&quot;,&quot;save&quot;:&quot;auto&quot;},&quot;fancybox&quot;:false,&quot;mediumzoom&quot;:false,&quot;lazyload&quot;:false,&quot;pangu&quot;:false,&quot;comments&quot;:{&quot;style&quot;:&quot;tabs&quot;,&quot;active&quot;:null,&quot;storage&quot;:true,&quot;lazyload&quot;:false,&quot;nav&quot;:null},&quot;motion&quot;:{&quot;enable&quot;:true,&quot;async&quot;:false,&quot;transition&quot;:{&quot;post_block&quot;:&quot;fadeIn&quot;,&quot;post_header&quot;:&quot;fadeInDown&quot;,&quot;post_body&quot;:&quot;fadeInDown&quot;,&quot;coll_header&quot;:&quot;fadeInLeft&quot;,&quot;sidebar&quot;:&quot;fadeInUp&quot;}},&quot;prism&quot;:false,&quot;i18n&quot;:{&quot;placeholder&quot;:&quot;Searching...&quot;,&quot;empty&quot;:&quot;We didn&#39;t find any results for the search: ${query}&quot;,&quot;hits_time&quot;:&quot;${hits} results found in ${time} ms&quot;,&quot;hits&quot;:&quot;${hits} results found&quot;}}</script><script src="/js/config.js"></script>
<meta name="description" content="第三方应用 由于StatusBarManager在SDK中没有提供，所以需要使用反射来调用该类的方法代码部分如下：  Object service &#x3D; getSystemService(&quot;statusbar&quot;); try &amp;#123;      Class  statusBarManager &#x3D; Class.forName(&quot;Android.app.StatusBarManager&quot;);">
<meta property="og:type" content="article">
<meta property="og:title" content=" Android 应用层面屏蔽状态栏下拉方法总结">
<meta property="og:url" content="http://www.nilstorm.com/2017/e7f2af2c.html">
<meta property="og:site_name" content="Lam&#39;s Blog">
<meta property="og:description" content="第三方应用 由于StatusBarManager在SDK中没有提供，所以需要使用反射来调用该类的方法代码部分如下：  Object service &#x3D; getSystemService(&quot;statusbar&quot;); try &amp;#123;      Class  statusBarManager &#x3D; Class.forName(&quot;Android.app.StatusBarManager&quot;);">
<meta property="og:locale">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg">
<meta property="article:published_time" content="2017-03-03T04:30:18.000Z">
<meta property="article:modified_time" content="2021-06-06T08:08:18.169Z">
<meta property="article:author" content="LinBinghe">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="状态栏">
<meta property="article:tag" content="源码">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg">


<link rel="canonical" href="http://www.nilstorm.com/2017/e7f2af2c.html">



<script class="next-config" data-name="page" type="application/json">{&quot;sidebar&quot;:&quot;&quot;,&quot;isHome&quot;:false,&quot;isPost&quot;:true,&quot;lang&quot;:&quot;zh-Hans&quot;,&quot;comments&quot;:true,&quot;permalink&quot;:&quot;http:&#x2F;&#x2F;www.nilstorm.com&#x2F;2017&#x2F;e7f2af2c.html&quot;,&quot;path&quot;:&quot;2017&#x2F;e7f2af2c.html&quot;,&quot;title&quot;:&quot; Android 应用层面屏蔽状态栏下拉方法总结&quot;}</script>

<script class="next-config" data-name="calendar" type="application/json">&quot;&quot;</script>
<title> Android 应用层面屏蔽状态栏下拉方法总结 | Lam's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Lam's Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lam's Blog</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Knowledge as Action</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LinBinghe"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">LinBinghe</p>
  <div class="site-description" itemprop="description">Knowledge as Action</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">60</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LinBinghe" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LinBinghe" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/shawn_lam" title="Zhihu → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;shawn_lam" rel="noopener" target="_blank"><i class="fab fa-zhihu fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:linbinghe@gmail.com" title="E-Mail → mailto:linbinghe@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="http://www.nilstorm.com/2017/e7f2af2c.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="LinBinghe">
      <meta itemprop="description" content="Knowledge as Action">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lam's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
           Android 应用层面屏蔽状态栏下拉方法总结
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2017-03-03 12:30:18" itemprop="dateCreated datePublished" datetime="2017-03-03T12:30:18+08:00">2017-03-03</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-06-06 16:08:18" itemprop="dateModified" datetime="2021-06-06T16:08:18+08:00">2021-06-06</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p><strong> 第三方应用 </strong><br>由于StatusBarManager在SDK中没有提供，所以需要使用反射来调用该类的方法<br>代码部分如下：</p>
<p><pre><code>
Object service = getSystemService("statusbar");
try &#123; 
    Class <?> statusBarManager = Class.forName("Android.app.StatusBarManager"); 
    Method expand = statusBarManager.getMethod("disable",int.class); 
    expand.invoke (service,0×00000001); 
&#125; catch (Exception e) &#123; 
    e.printStackTrace(); 
&#125;
</code></pre><br><span id="more"></span><br>禁用通知栏需要添加以下权限并使用系统签名：</p>
<p><pre><code></p>
<p><uses-permission android:name="android.permission.STATUS_BAR"/> </p>
<p><uses-permission android:name="android.permission.DISABLE_STATUS_BAR"/><br>&lt;/code&gt;&lt;/pre&gt;<br>如果没有使用系统签名则会报如下错误：</p>
<p><blockquote>Caused by: Java.lang.SecurityException: StatusBarManagerService: Neither user 10072 nor current process has android.permission.STATUS_BAR.</blockquote><br>在使用系统签名之前需要：</p>
<ul>
<li>在应用程序的AndroidManifest.xml中的manifest节点中加入android:sharedUserId=”android.uid.system”</li>
<li>修改Android.mk文件，加入LOCAL_CERTIFICATE:=platform</li>
</ul>
<p>让程序运行到系统进程中，Android中使应用程序获得系统签名权限方法：</p>
<ul>
<li>需要在Android系统源码的环境下用make来编译<ul>
<li>在应用程序的AndroidManifest.xml中的manifest节点中加入android:sharedUserId=”android.uid.system”</li>
<li>修改Android.mk文件，加入LOCAL_CERTIFICATE:=platform</li>
<li>使用mm命令来编译生成APK即可</li>
</ul>
</li>
<li>不需要在系统源码环境下编译<ul>
<li>在应用程序的AndroidManifest.xml中的manifest节点中加入android:sharedUserId=”android.uid.system”</li>
<li>使用eclipse编译出APK文件，此时的APK文件不可用</li>
<li>用压缩软件打开APK文件，删掉META-INF目录下的CERT.SF和CERT.RSA两个文件</li>
<li>使用目标系统的platform密钥来重新给APK文件签名。首先要找到密钥文件，如”build\target\product\security”，下面的platform.pk8和platform.x509.pem两个文件，然后用Android提供的SignApk工具来签名，该工具源码在”build\tools\signapk”下，用法是”signapk platform.x509.pem platform.pk8 input.apk output.apk”<br>原理：<br>首先加入android:sharedUserId=”android.uid.system”这个属性，通过SharedUserId拥有同一个User ID的多个APK可以配置成运行在同一个进程中。那么把程序的UID配成android.uid.system，也就是要让程序运行在系统进程中，就具有相应的权限了。<br>但是只是加入UID还不够，这时候的APK是无法安装的，因为签名不符。程序想要运行在系统进程中还要有目标系统的platform key，就是上面第二个方法中的platform.pk8和platform.x509.pem两个文件。用这两个key签名后apk才真正可以放入系统进程中，第一个方法的LOCAL_CERTIFICATE:=platform其实就是用这两个key签名<br>这也有一个问题，就是这样生成的程序只有在原始的Android系统或自己编译的系统中才可用，因为这样的系统才可以拿到platform.pk8和platform.x509.pem两个文件，要是别家公司的Android系统连安装都安装不了。目前只是整理了资料，还没有进行实际开发，之后如果有实际应用这部分内容，会将最后结果整理发布。</li>
</ul>
</li>
</ul>
<p><strong> 系统级应用 </strong><br>如果是系统级应用，也就是手机厂家植入的应用，可以使用disable(int)的方法来进行屏蔽，参数如上源码五个参数之一即可。但是如果是在应用层上的，disable方法因为权限问题无法使用（如果一定要使用必须具有系统签名）。这个时候可以使用collapse()方法，现在的小米锁屏和360锁屏都是使用该方法，具体代码如下：</p>
<p><pre><code>
public void disableStatusBar()&#123;  
    try &#123;  
        Object service = getSystemService("statusbar");  
        Class<?> claz = Class.forName("android.app.StatusBarManager");  
        Method expand = claz.getMethod("collapse");  
        expand.invoke(service);  
    &#125; catch (Exception e) &#123;  
        e.printStackTrace();  
    &#125;  
&#125;
</code></pre><br>重写activity的onWindowfocuschanged方法，执行如上操作即可。但是此种方法有严重的适配性问题，不同的系统下的回滚方法名不一样，需要对所有版本进行适配，非常繁琐。</p>
<hr>
<p><strong>版权声明</strong></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg" alt="Creative Commons BY-NC-ND 4.0 International License"></p>
<p><a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog</a> by <a target="_blank" rel="noopener" href="http://linbinghe.me/">Binghe Lin</a> is licensed under a <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons BY-NC-ND 4.0 International License</a>.<br>由<a target="_blank" rel="noopener" href="http://linbinghe.me/">林炳河</a>创作并维护的<a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog</a>采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">创作共用保留署名-非商业-禁止演绎4.0国际许可证</a>。</p>
<p>本文首发于<a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog - Knowledeg as Action</a>，版权所有，侵权必究。</p>
<p>本文永久链接：<a target="_blank" rel="noopener" href="http://codinglife.me/2017/e7f2af2c.html">http://codinglife.me/2017/e7f2af2c.html</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/%E7%8A%B6%E6%80%81%E6%A0%8F/" rel="tag"># 状态栏</a>
              <a href="/tags/%E6%BA%90%E7%A0%81/" rel="tag"># 源码</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2017/f904729.html" rel="prev" title="Android拦截电源键长按">
                  <i class="fa fa-chevron-left"></i> Android拦截电源键长按
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2017/55efffcd.html" rel="next" title="利用bat实现Android的自动化编译">
                  利用bat实现Android的自动化编译 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LinBinghe</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  






  





</body>
</html>
